package com.sport.utils;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ObjectUtils {

    public static <T> void removeNullFields(T obj) throws IllegalAccessException {
    if (obj == null) {
        return;
    }
    Class<?> clazz = obj.getClass();
    for (Field field : clazz.getDeclaredFields()) {
        field.setAccessible(true);
        if (field.get(obj) == null) {
            field.set(obj, null); // 保持为null，后续处理会移除
        }
    }
}


    public static <T> List<T> removeNullFieldsFromList(List<T> list) throws IllegalAccessException {
    List<T> resultList = new ArrayList<>();
    for (T obj : list) {
        removeNullFields(obj);
        if (!containsNullFields(obj)) { // 检查对象是否包含null字段
            resultList.add(obj);
        }
    }
    return resultList;
}

    private static <T> boolean containsNullFields(T obj) throws IllegalAccessException {
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            if (field.get(obj) == null) {
                return true;
            }
        }
        return false;
    }

}

